//
// Created by imagination on 3/31/25.
//
#include "home_page.h"
#include "setting_page.h"
#include "application.h"

static uint32_t col_desc[] = { LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
static uint32_t row_desc[] = { LV_GRID_FR(9), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };

static void setting_card_clicked(lv_event_t * e) {
    lv_fragment_t * fragment = lv_fragment_create(&setting_fragment_cls, NULL);
    lv_fragment_manager_push(app.manager, fragment, &app.container);
}

static void carplay_card_clicked(lv_event_t * e) {
    LV_LOG_USER("carplay_card_clicked: %d", lv_event_get_code(e));
}

static void home_button_clicked(lv_event_t * e) {
    lv_fragment_manager_pop(app.manager);
}

static void container_delete_cb(lv_event_t * e) {
    lv_fragment_manager_t * manager = (lv_fragment_manager_t *) lv_event_get_user_data(e);
    lv_fragment_manager_delete(manager);
}

void home_page_show(void) {
    lv_obj_t * root = lv_obj_create(lv_screen_active());
    lv_obj_set_style_border_width(root, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_all(root, 0, LV_PART_MAIN);
    lv_obj_set_style_outline_width(root, 0, LV_PART_MAIN);
    lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));
    lv_obj_set_layout(root, LV_LAYOUT_GRID);
    lv_obj_set_grid_dsc_array(root, col_desc, row_desc);
    lv_obj_remove_flag(root, LV_OBJ_FLAG_SCROLLABLE);

    app.manager = lv_fragment_manager_create(NULL);
    app.container = lv_obj_create(root);
    lv_obj_remove_style(app.container, NULL, LV_PART_MAIN);
    lv_obj_set_grid_cell(app.container, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 0, 1);
    lv_obj_add_event_cb(root, container_delete_cb, LV_EVENT_DELETE, app.manager);

    lv_obj_t * content_container = lv_obj_create(app.container);
    lv_obj_set_layout(content_container, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(content_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_size(content_container, LV_PCT(100), LV_PCT(100));

    static lv_style_t card_style;
    lv_style_init(&card_style);
    lv_style_set_radius(&card_style, 8);
    lv_style_set_bg_color(&card_style, lv_palette_main(LV_PALETTE_GREEN));
    lv_style_set_bg_opa(&card_style, LV_OPA_100);
    lv_style_set_shadow_width(&card_style, 20);
    lv_style_set_shadow_color(&card_style, lv_palette_darken(LV_PALETTE_GREY, 3));

    lv_obj_t * setting_card = lv_obj_create(content_container);
    lv_obj_add_style(setting_card, &card_style, LV_PART_MAIN);
    lv_obj_set_size(setting_card, 200, 300);
    lv_obj_add_event_cb(setting_card, setting_card_clicked, LV_EVENT_CLICKED, NULL);

    lv_obj_t * carplay_card = lv_obj_create(content_container);
    lv_obj_add_style(carplay_card, &card_style, LV_PART_MAIN);
    lv_obj_set_size(carplay_card, 200, 300);
    lv_obj_add_event_cb(carplay_card, carplay_card_clicked, LV_EVENT_CLICKED, NULL);


    lv_obj_t * nav_container = lv_obj_create(root);
    lv_obj_set_layout(nav_container, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(nav_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_grid_cell(nav_container, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);

    lv_obj_t * home_button = lv_button_create(nav_container);
    lv_obj_t * label = lv_label_create(home_button);
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_label_set_text(label, "home");
    lv_obj_add_event_cb(home_button, home_button_clicked, LV_EVENT_CLICKED, NULL);
}